package com.chenjiajin.fastjson_test.serializerFeature;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chenjiajin.domain.TestPoJo;
import com.chenjiajin.domain.TestPoJo2;

public class ListTest {

    // 演示: Fastjson 转换出现 $ref 问题解决  , jackson则没有这个问题!!!
    // 实体转化为json字符串后出现了 $ref 字样的东西，这是因为在传输的数据中出现相同的对象  fastjson默认开启引用检测将相同的对象写成引用的形式
    public static void main(String[] args) {
        TestPoJo2 outside = new TestPoJo2();
        TestPoJo inside = new TestPoJo("as1111das", "fhgfgh", "utyuty", "opipi");

        outside.setName(inside);
        outside.setHobby(inside);   //多次引用同一对象
        outside.setArgs(inside);
        outside.setPath(inside);
        String jsonStr = JSON.toJSONString(outside);

        System.err.println("jsonStr = " + jsonStr);
        /**
         *  {
         *     "args": {
         *         "args": "opipi",
         *         "hobby": "utyuty",
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     },
         *     "hobby": {
         *         "$ref": "$.args"
         *     },
         *     "name": {
         *         "$ref": "$.args"
         *     },
         *     "path": {
         *         "$ref": "$.args"
         *     }
         * }
         */

        //解决:
        String jsonStr2 = JSON.toJSONString(outside, SerializerFeature.DisableCircularReferenceDetect);
        System.err.println("jsonStr2 = " + jsonStr2);
        /**
         *  {
         *     "args": {
         *         "args": "opipi",
         *         "hobby": "utyuty",
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     },
         *     "hobby": {
         *         "args": "opipi",
         *         "hobby": "utyuty",
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     },
         *     "name": {
         *         "args": "opipi",
         *         "hobby": "utyuty",
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     },
         *     "path": {
         *         "args": "opipi",
         *         "hobby": "utyuty",
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     }
         * }
         */

    }

}